﻿using Haidou.Wpf.Util.Settings;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows;

namespace Haidou.Wpf.Util
{
	/// <summary>
	///保存有关正在运行的实例的当前参数的信息。
	/// </summary>
	public static class Arguments
	{
		#region Properties



		static Arguments()
		{

		}






		/// <summary>
		/// 系统环境详情
		/// </summary>
		public static string SystemTypeDetails = "";


		/// <summary>
		/// 当前系统环境是64位吗
		/// </summary>
		public static bool Is64BitOperatingSystem { get; set; }


		/// <summary>
		/// 当前应用进程是64位程序吗
		/// </summary>
		public static bool Is64BitProcess { get; set; }




		/// <summary>
		/// 网络状态
		/// </summary>
		public static bool NetworkStatus = true;



		/// <summary>
		/// 长度
		/// </summary>
		public static int HistoryTranslatedValueLength;




		/// <summary>
		///作为参数传递给此可执行文件的文件路径。
		///只有存在的文件才不会被忽略。
		/// </summary>
		public static List<string> FileNames { get; set; } = new List<string>();

		/// <summary>
		/// 如果此实例不应试图显示除下载窗口之外的任何内容，则为True。
		/// </summary>
		public static bool IsInDownloadMode { get; set; }

		/// <summary>
		/// 应该发生的下载类型(Gifski、FFmpeg、SharpDX)。
		/// </summary>
		public static string DownloadMode { get; set; }

		/// <summary>
		/// 下载的输出路径。
		/// </summary>
		public static string DownloadPath { get; set; }


		/// <summary>
		/// 日志的输出路径。
		/// </summary>
		public static string LogsPath { get; set; }


		/// <summary>
		///如果此实例除了尝试将设置保存到磁盘之外，不应尝试显示任何内容，则为True。
		/// </summary>
		public static bool IsInSettingsMode { get; set; }

		/// <summary>
		/// 忽略单实例设置，继续打开应用程序的新实例。
		/// </summary>
		public static bool NewInstance { get; set; }

		/// <summary>
		/// 打开一个窗口。
		/// </summary>
		public static bool Open { get; set; }

		/// <summary>
		/// 用-open命令打开的窗口。
		/// </summary>
		public static int WindownToOpen { get; set; }



		/// <summary>
		/// The capture limit.
		/// </summary>
		public static TimeSpan Limit { get; set; }

		/// <summary>
		/// True if the recorder should start capture right away.
		/// </summary>
		public static bool StartCapture { get; set; }


		#endregion

		/// <summary>
		/// 准备
		/// </summary>
		/// <param name="args"></param>
		public static void Prepare(string[] args)
		{
			FileNames.Clear();

			for (int i = 0; i < args.Length; i++)
			{
				switch (args[i])
				{
					case "/lang":
					case "-lang":
						{
							//更改应用的语言，例如:-lang pt
							if (args.Length > i + 1)
							{
								try
								{
									//如果语言设置不正确，会自动失败。
									UserSettings.All.LanguageCode = new CultureInfo(args[i + 1]).ThreeLetterISOLanguageName;
									i++;
								}
								catch (Exception e)
								{
									LogWriter.Log(e, $"The language code {args[i + 1]} was not recognized.");
								}
							}

							break;
						}

					case "-d":
					case "/d":
					case "-download":
					case "/download":
						{
							if (args.Length > i + 2)
							{
								IsInDownloadMode = true;
								i++;

								DownloadMode = args[i++];
								DownloadPath = args[i++];
							}
							break;
						}

					case "-sm":
					case "/sm":
					case "-softmode":
					case "/softmode":
						{
							//强制使用软件模式。
							UserSettings.All.DisableHardwareAcceleration = true;
							break;
						}

					case "-hm":
					case "/hm":
					case "-hardmode":
					case "/hardmode":
						{
							//强制使用硬件模式。
							UserSettings.All.DisableHardwareAcceleration = false;
							break;
						}

					case "-settings":
						{
							//启用尝试使用管理权限保存设置的模式。
							IsInSettingsMode = true;
							break;
						}

					case "-n":
					case "/n":
					case "/new":
					case "-new":
						{
							NewInstance = true;
							break;
						}

					case "-o":
					case "/o":
					case "/open":
					case "-open":
						{
							if (args.Length <= i + 1)
							{
								return;
							}

							//-open screen-recorder(webcam-recorder/board-recorder/editor/options/startup/minimized)//打开屏幕记录器(///选项/启动/最小化)
							Open = true;

							#region Get window to open

							string window = args[++i];

							switch (window)
							{
								case "m":
								case "min":
								case "minimized":
									WindownToOpen = -1;
									break;

								case "up":
								case "start":
								case "startup":
									WindownToOpen = 0;
									break;

								case "s":
								case "screen":
								case "screen-recorder":
									WindownToOpen = 1;
									break;

								case "w":
								case "webcam":
								case "webcam-recorder":
									WindownToOpen = 2;
									break;

								case "b":
								case "board":
								case "board-recorder":
									WindownToOpen = 3;
									break;

								case "e":
								case "editor":
									WindownToOpen = 4;
									break;

								case "o":
								case "options":
									WindownToOpen = 5;
									break;

								default:
									Open = false;
									break;
							}

							#endregion

							break;
						}

					case "-r":
					case "/r":
					case "/region":
					case "-region":
						{
							try
							{
								//-地区/-r 100,50,500,200
								if (args.Length > i + 1)
								{

								}
							}
							catch (Exception ex)
							{
								LogWriter.Log(ex, "Not possible to parse the capture rectangle from arguments", args[i++]);//无法从参数中解析捕获矩形
							}

							break;
						}

					case "-f":
					case "/f":
					case "/framerate":
					case "-framerate":
					case "/frequency":
					case "-frequency":
						{
							if (args.Length <= i + 1)
							{
								return;
							}

							//-framerate/-f (60fps/60fpm/60fph/manual/interaction)//帧频
							ParseFramerate(args[++i].Trim());
							break;
						}

					case "-l":
					case "/l":
					case "/limit":
					case "-limit":
						{
							//-limit/-l 01:30
							if (args.Length <= i + 1)
							{
								return;
							}

							if (TimeSpan.TryParse(args[++i].Trim(), CultureInfo.InvariantCulture, out TimeSpan time))
							{
								Limit = time;
							}

							break;
						}

					case "-c":
					case "/c":
					case "/capture":
					case "-capture":
						{
							StartCapture = true;
							break;
						}

					default:
						{
							string path = args[i].Trim('"').Trim('\'');

							//Anything else is treated as file to be imported.//其他任何内容都被视为要导入的文件。
							if (File.Exists(path))
							{
								FileNames.Add(path);
							}

							break;
						}
				}
			}
		}

		public static void ClearAutomationArgs()
		{
			Open = false;
			WindownToOpen = 0;
		}


		private static void ParseFramerate(string frequency)
		{
			
		}

		private static void ParseFramerate()
		{
			
		}

	}
}
